﻿using System.Globalization;
using System.Management;
using System.Windows.Forms;
using Microsoft.Win32;
using NLog.Fluent;

namespace Labthink.PCSoft.UniversalHost4.Utility
{
    public static class SystemInfo
    {
        public static string Processor { get; set; }

        public static string OsName { get; set; }

        public static string Sp { get; set; }

        public static string Version { get; set; }

        public static string ReleaseId { get; set; }

        public static string Architecture { get; set; }

        public static string Locale { get; set; }

        public static string LocaleInConfig { get; set; }

        public static string Resolution { get; set; }

        static SystemInfo()
        {
            GetOperatingSystemInfo();
            GetProcessorInfo();
            GetOsCultureName();
            GetScreeSize();
        }

        private static void GetScreeSize()
        {
            var width = Screen.PrimaryScreen.Bounds.Width;
            var height = Screen.PrimaryScreen.Bounds.Height;
            Resolution= $"{width}*{height}";
        }

        public static void GetOperatingSystemInfo()
        {
            var mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
            foreach (ManagementObject managementObject in mos.Get())
            {
                if (managementObject["Caption"] != null)
                {
                    OsName= managementObject["Caption"].ToString();
                }

                if (managementObject["OSArchitecture"] != null)
                {
                    Architecture = managementObject["OSArchitecture"].ToString();
                }

                if (managementObject["CSDVersion"] != null)
                {
                    Sp= managementObject["CSDVersion"].ToString();
                }

                if (managementObject["Version"] != null)
                {
                    Version = managementObject["Version"].ToString();
                }

                ReleaseId = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
            }
        }

        public static void GetProcessorInfo()
        {
            RegistryKey processorName = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree);

            if (processorName?.GetValue("ProcessorNameString") != null)
            {
                Processor = processorName.GetValue("ProcessorNameString").ToString();
            }
        }

        public static void GetOsCultureName()
        {
            CultureInfo ci = CultureInfo.InstalledUICulture;
            Locale = ci.Name;
        }

        /// <summary>
        /// 获取当前系统的locale
        /// 优先级：用户配置文件，系统locale
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentLocale()
        {
            if (!string.IsNullOrEmpty(LocaleInConfig))
            {
                Log.Trace().Message($"Use locale in settings: {LocaleInConfig}").Write();
                return LocaleInConfig;
            }

            Log.Trace().Message($"Use locale of system: {Locale}").Write();
            return Locale;
        }
    }
}

